#!/usr/bin/env python
# -*- coding: utf-8 -*-

from random import randint
from register import Favor
import time

def register_new():
    emailfp = open('email.txt')
    passwordfp = open('password.txt')
    namefp = open('new_name.txt')
    collagefp = open('collages.txt')

    emails = emailfp.readlines()
    passwords = passwordfp.readlines()
    names = namefp.readlines()
    collages = collagefp.readlines()

    for i in range(10):
        email = emails[randint(0, len(emails)-1)].strip()
        password = passwords[randint(0, len(passwords)-1)].strip()
        name = names[randint(0, len(names)-1)].strip()
        collage = collages[randint(0, len(collages)-1)].strip()

        # print email+'-'+password+'-'+name+'-'+collage
        A = Favor()
        A.register(email, password, name, collage)
        login(A)
        Zan(A)

def login(A):
    A.login()

def Zan(A):
    for j in ['43', '99', '110']:
        A.PraiseDaily_auto(j)

def login_old():
    
    day_of_month = time.localtime().tm_mday
    month = time.localtime().tm_mon
    status = str(day_of_month) + "_" + str(month) 
    
    newfp = open('zan'+status+'.log', 'a+')
    newfplist = newfp.readlines()

    oldfp = open('userlist')
    # oldfp = open('userlist_day')
    userlist = oldfp.readlines()
    for user in userlist:
        user = userlist[randint(0, len(userlist)-1)]
        username, password, name, collage = user.split(' ')
        # username, password, oldstatus = user.split(' ')
        username = username.strip()
        password = password.strip()

        # 投玩票记录新状态，防止今天继续投票
        message = username+" "+password+" "+"status="+status+'\n'
        if message in newfplist:
            print username + '已登入过今天'
            continue
        print username + '今天第一次登录'
        newfplist.append(message)
        # 保存新数据
        with open('zan'+status+'.log', 'a+') as tp:
            tp.write(message)

        A = Favor()
        A.login(username, password)
        for j in ['317', '43', '232', '110', '169']:
            print j, '-', 
            A.PraiseDaily_auto(j)
            time.sleep(randint(8,25))


        print ""
        t = randint(20, 55)
        print "seconds: " + str(t)
        time.sleep(t)
    oldfp.close()

if __name__ == '__main__':
    dids = Favor()
    dids.get_daily_id(38)
    login_old()
    #register_new()
